package com.ioudi.digu.flex.config.xml;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.core.Conventions;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

public class ParsingUtils
{
	 static int countProvidedAttributeValues(Element element, String... values) {
	        int count = 0;
	        for (String s : values) {
	            if (StringUtils.hasText(element.getAttribute(s))) {
	                count++;
	            }
	        }
	        return count;
	    }

	    static void mapAllAttributes(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
	        new SimpleBeanDefinitionParser().mapToBuilder(element, parserContext, builder);
	    }

	    static void mapOptionalAttributes(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, String... attrs) {
	        for (String attr : attrs) {
	            String value = element.getAttribute(attr);
	            if (StringUtils.hasText(value)) {
	                String propertyName = Conventions.attributeNameToPropertyName(attr);
	                if (validateProperty(element, parserContext, propertyName, attr)) {
	                    builder.addPropertyValue(propertyName, value);
	                }
	            }
	        }
	    }

	    static void mapOptionalBeanRefAttributes(Element element, BeanDefinitionBuilder builder, ParserContext parserContext, String... attrs) {
	        for (String attr : attrs) {
	            String value = element.getAttribute(attr);
	            if (StringUtils.hasText(value)) {
	                String propertyName = Conventions.attributeNameToPropertyName(attr);
	                if (validateProperty(element, parserContext, propertyName, attr)) {
	                    builder.addPropertyReference(propertyName, value);
	                }
	            }
	        }
	    }

	    static void mapRequiredAttributes(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, String... attrs) {
	        for (String attr : attrs) {
	            String value = element.getAttribute(attr);
	            if(!validateRequiredAttribute(element,parserContext,attr)) {
	                return;
	            }
	            String propertyName = Conventions.attributeNameToPropertyName(attr);
	            if (validateProperty(element, parserContext, propertyName, attr)) {
	                builder.addPropertyValue(propertyName, value);
	            }
	        }
	    }

	    static void mapRequiredBeanRefAttributes(Element element, ParserContext parserContext, BeanDefinitionBuilder builder, String... attrs) {
	        for (String attr : attrs) {
	            String value = element.getAttribute(attr);
	            if(!validateRequiredAttribute(element,parserContext,attr)) {
	                return;
	            }
	            String propertyName = Conventions.attributeNameToPropertyName(attr);
	            if (validateProperty(element, parserContext, propertyName, attr)) {
	                builder.addPropertyReference(propertyName, value);
	            }
	        }
	    }

	    static String registerInfrastructureComponent(Element element, ParserContext parserContext, BeanDefinitionBuilder componentBuilder) {
	        String beanName = parserContext.getReaderContext().generateBeanName(componentBuilder.getRawBeanDefinition());
	        ParsingUtils.registerInfrastructureComponent(element, parserContext, componentBuilder, beanName);
	        return beanName;
	    }

	    static void registerInfrastructureComponent(Element element, ParserContext parserContext, BeanDefinitionBuilder componentBuilder, String beanName) {
	        componentBuilder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
	        componentBuilder.getRawBeanDefinition().setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
	        parserContext.registerBeanComponent(new BeanComponentDefinition(componentBuilder.getBeanDefinition(), beanName));
	    }
	    
	    private static boolean validateRequiredAttribute(Element element, ParserContext parserContext, String attr) {
	        if (!StringUtils.hasText(attr)) {
	            parserContext.getReaderContext().error("The '" + attr + "' attribute is required.", parserContext.extractSource(element));
	            return false;
	        }
	        return true;
	    }

	    private static boolean validateProperty(Element element, ParserContext parserContext, String propertyName, String attr) {
	        if (!StringUtils.hasText(propertyName)) {
	            parserContext.getReaderContext().error(
	                "Illegal property name trying to convert from attribute '" + attr + "' : cannot be null or empty.",
	                parserContext.extractSource(element));
	            return false;
	        }
	        return true;
	    }

	    private static class SimpleBeanDefinitionParser extends AbstractSimpleBeanDefinitionParser {

	        protected void mapToBuilder(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
	            try {
	                this.doParse(element, builder);
	            } catch (Exception ex) {
	                parserContext.getReaderContext().error(ex.getMessage(), parserContext.extractSource(element), ex);
	            }
	        }
	    }
}
